package fr.utbm.gi.in56.user;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fr.utbm.gi.in56.database.DatabaseServlet;
import fr.utbm.gi.in56.error.ErrorType;
import fr.utbm.gi.in56.notification.NotificationType;

/**
 * Servlet implementation class EditServlet*
 * 
 * @author Adrien Hoffer & Tarik Belabbas
 */
@WebServlet("/EditServlet")
public class EditServlet extends DatabaseServlet
{
	private static final long	serialVersionUID	= 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EditServlet()
	{
		super();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException
	{
		request.setCharacterEncoding( "UTF-8" ); //$NON-NLS-1$
		// Get request parameter
		int county = Integer.parseInt( request.getParameter( "county" ) ); //$NON-NLS-1$
		String newPassword1 = request.getParameter( "newPassword1" ); //$NON-NLS-1$
		String newPassword2 = request.getParameter( "newPassword2" ); //$NON-NLS-1$
		
		// Get the current user
		HttpSession session = request.getSession( true );
		UserBean user = (UserBean)session.getAttribute( "currentSessionUser" ); //$NON-NLS-1$
		
		boolean countyChange = ( county != user.getCounty() );
		boolean passwordChange = ( !newPassword1.isEmpty() && !newPassword2.isEmpty() );

		// If the user didn't request at least one change...
		if ( !countyChange && !passwordChange )
		{
			response.sendRedirect("editProfile.jsp"); //error page 
			return;
		}
		else
		{
			// ...then the password must be filled
			String password = request.getParameter( "password" ); //$NON-NLS-1$
			if ( password.isEmpty() )
			{
				returnError( request, response, ErrorType.EDIT_PASSWORD_EMPTY );
				return;
			}
			else
			{
				UserManager userMg = new UserManager( getConnection() );
				ErrorType result = ErrorType.NONE;
				
				if ( countyChange )
				{
					result = userMg.setCounty( user, password, county );
					
					if ( result != ErrorType.NONE )
					{
						returnError( request, response, result );
						return;
					}
				}
				
				if ( passwordChange )
				{
					if ( newPassword1.compareTo( newPassword2 ) != 0 )
					{
						returnError( request, response, ErrorType.EDIT_NON_MATCHING_PASSWORDS );
						return;
					}
					else
					{
						result = userMg.setPassword( user.getLogin(), password, newPassword1 );
						if ( result != ErrorType.NONE )
						{
							returnError( request, response, result );
							return;
						}
					}
				}
									
				request.setAttribute( "notification", NotificationType.EDIT_SUCCESS ); //$NON-NLS-1$
				RequestDispatcher rd = request.getRequestDispatcher("notification.jsp"); //$NON-NLS-1$
				rd.forward( request, response );
				return;
			}
		}
	}
	
	/**
	 * Sets the error as request attribute and forward to previous page.
	 * 
	 * @param _error the error
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void returnError( HttpServletRequest request,
			HttpServletResponse response, ErrorType _error ) throws ServletException, IOException
	{
		request.setAttribute( "editError", _error ); //$NON-NLS-1$
		RequestDispatcher rd = request.getRequestDispatcher("editProfile.jsp"); //$NON-NLS-1$
		rd.forward( request, response );
	}
}
